/**
 * ElementUI npm i element-ui -S
 * */
import ElementUI from 'element-ui'
import Clipboard from 'clipboard'

let commons = {};

/**
 * 复制
 * data复制的数据，
 * obj按钮id
 * */
commons.copy = function copeLink(data,obj) {
  let copy = data;
  let clipboard = new Clipboard(obj, {
    text: function () {
      return copy;
    }
  });
  clipboard.on('success', function (e) {
    ElementUI.Message({
      message: '复制成功',
      type: 'success',
      duration: 1400,
      offset: 300
    });
  });
};

/**
 * 提示弹窗
 * tip提示内容
 * */
commons.popup = function popups(tip) {
  ElementUI.MessageBox(tip, '提示', {
    confirmButtonText: '确定',
    callback: action => {
    }
  })
};

/**
 * 日期格式化
 * fmt格式如：yyyy-MM-dd
 * date日期
 * */
commons.formatTime = function dateFtt(time) {
  let dateee = new Date(time).toJSON();
  return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
};

// 加密
commons.encode = function encode(str) {
  return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
    function toSolidBytes(match, p1) {
      return String.fromCharCode('0x' + p1);
    }));
};

// 解密
commons.decode = function decode(str) {
  return decodeURIComponent(atob(str).split('').map(function (c) {
    return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  }).join(''));
};

// 手机校验
// commons.judgePhone = function judgePhone(value) {
//   let pPattern = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
//   return pPattern.test(value);
// }

// 电话
commons.judgeLandline = function judgePhone(value) {
  let phPattern = /^[0][1-9]{2,3}-[0-9]{5,10}$/;
  return phPattern.test(value)
}

// 手机、座机校验
commons.judgePhone = function judgePhone(value) {
  let phone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  let tel =  /^(\d{3,4}-)?\d{7,8}$/i;
  return tel.test(value) || phone.test(value);
}


// 名称校验（中文或英文）
commons.judgeName = function judgeName(value){
  let nPattern =  /^[\u4e00-\u9fa5]{0,}$/;
  let ePattern =  /^[a-zA-Z]$/;
  return nPattern.test(value)||ePattern.test(value);
}

// 名称校验（中文英文数字）
commons.judgeInfo = function judgeInfo(value){
  let nPattern = /^[\u4E00-\u9FA5A-Za-z0-9]+$/
  return !nPattern.test(value);
}

// 邮箱校验
commons.judgeEmail = function judgeEmail(email) {
  let emPattern = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*\.[a-zA-Z0-9]{2,6}$/;
  return !emPattern.test(email);

}

commons.judgeCard = function judgeCard(card) {
  let cpattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  return !cpattern.test(card);
}
//验证车牌号
commons.judgeCarID = function judgeCarID(id) {
  let idParren = /^([京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][1-9DF][1-9ABCDEFGHJKLMNPQRSTUVWXYZ]\d{3}[1-9DF]|[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][\dABCDEFGHJKLNMxPQRSTUVWXYZ]{5})$/
  return !idParren.test(id);
}
//验证密码长度大于6且由
commons.judgePwd = function judgePwd(pwd) {
  var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/;

    return !reg.test(pwd);
}

export default commons


